/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

// Fig. 28.28: DisplayQueryResults.java
// Display the contents of the Authors table in the books database.
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.PatternSyntaxException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableModel;

class DisplayQueryResults extends JFrame {
    // database URL, username and password

    static final String DATABASE_URL = "jdbc:mysql://localhost/chat";
    static final String USERNAME = "root";
    static final String PASSWORD = "root";
// default query retrieves all data from Authors table
    static final String DEFAULT_QUERY = "SELECT * FROM account";
    private ResultSetTableModel tableModel;
    private JTextArea queryArea;
    private JTable resultTable;
    // create ResultSetTableModel and GUI

    public DisplayQueryResults() {
        super("Displaying Query Results");

        // create ResultSetTableModel and display database table
        try {
            // create TableModel for results of query SELECT * FROM Authors
            tableModel = new ResultSetTableModel(DATABASE_URL, USERNAME, PASSWORD, DEFAULT_QUERY);

            // set up JTextArea in which user types queries
            queryArea = new JTextArea(DEFAULT_QUERY, 3, 100);
            queryArea.setWrapStyleWord(true);
            queryArea.setLineWrap(true);

            JScrollPane scrollPane = new JScrollPane(queryArea,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

            // set up JButton for submitting queries
            JButton submitButton = new JButton("Submit Query");
            // set up JButton for submitting queries
            /// JButton jbtAddRow=new JButton("add new row";
            // create Box to manage placement of queryArea and
            // submitButton in GUI
            Box boxNorth = Box.createHorizontalBox();
            boxNorth.add(scrollPane);
            boxNorth.add(submitButton);

// create JTable based on the tableModel
            // JTable resultTable = new JTable(tableModel);
            resultTable = new JTable(tableModel);
            resultTable.setCellSelectionEnabled(true);
            resultTable.setRowSelectionAllowed(true);
            resultTable.setColumnSelectionAllowed(true);

            JLabel filterLabel = new JLabel("Filter:");
            final JTextField filterText = new JTextField();
            JButton filterButton = new JButton("Apply Filter");
            Box boxSouth = Box.createHorizontalBox();

            boxSouth.add(filterLabel);
            boxSouth.add(filterText);
            boxSouth.add(filterButton);

            // place GUI components on content pane
            add(boxNorth, BorderLayout.NORTH);
            add(new JScrollPane(resultTable), BorderLayout.CENTER);
            add(boxSouth, BorderLayout.SOUTH);

            resultTable.getModel().addTableModelListener(new TableModelListener() {
                @Override
                public void tableChanged(TableModelEvent tme) {

                    int row = tme.getFirstRow();
                    int column = tme.getColumn();

                    if (!(row > tableModel.getRowCount() || column > tableModel.getColumnCount()) && !(row < 0)) {
                        String upQuery = "update account set " + tableModel.colname + "='" + tableModel.rowData[row][column].toString() + "' where id=" + tableModel.getValueAt(row, 0);

                        try {
                            tableModel.updateQuery(upQuery);
                            tableModel.setQuery(DEFAULT_QUERY);
                            JOptionPane.showMessageDialog(null, "updating is saved");
                        } catch (SQLException ex) {
                            Logger.getLogger(DisplayQueryResults.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalStateException ex) {
                            Logger.getLogger(DisplayQueryResults.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });
            Box boxwestNorth = Box.createVerticalBox();
            JButton AccountsButton = new JButton("accounts");
            boxwestNorth.add(AccountsButton, BorderLayout.NORTH);
            AccountsButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    queryArea.setText("select * from account");
                }
            });

            JButton messageButton = new JButton("All Messages");
            boxwestNorth.add(messageButton, BorderLayout.CENTER);
            messageButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    queryArea.setText("select * from message");
                }
            });
            JButton ContactList = new JButton("contactList");
            boxwestNorth.add(ContactList, BorderLayout.SOUTH);
            ContactList.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    queryArea.setText("select * from contactlist");
                }
            });
            add(boxwestNorth, BorderLayout.EAST);
            // create event listener for submitButton
            submitButton.addActionListener(
                    new ActionListener() {
                        // pass query to table model
                        public void actionPerformed(ActionEvent event) {
                            // perform a new query
                            try {
                                tableModel.setQuery(queryArea.getText());
                            } // end try
                            catch (SQLException sqlException) {
                                JOptionPane.showMessageDialog(null,
                                        sqlException.getMessage(), "Database error",
                                        JOptionPane.ERROR_MESSAGE);

                                // try to recover from invalid user query
                                // by executing default query
                                try {
                                    tableModel.setQuery(DEFAULT_QUERY);
                                    queryArea.setText(DEFAULT_QUERY);
                                } // end try
                                catch (SQLException sqlException2) {
                                    JOptionPane.showMessageDialog(null,
                                            sqlException2.getMessage(), "Database error",
                                            JOptionPane.ERROR_MESSAGE);

// ensure database connection is closed
                                    tableModel.disconnectFromDatabase();
                                    System.exit(1); // terminate application
                                } // end inner catch
                            } // end outer catch
                        } // end actionPerformed
                    } // end ActionListener inner class
                    ); // end call to addActionListener



            final TableRowSorter< TableModel> sorter = new TableRowSorter< TableModel>(tableModel);
            resultTable.setRowSorter(sorter);


            setSize(500, 250); // set window size
            setVisible(true); // display window

            // create listener for filterButton
            filterButton.addActionListener(
                    new ActionListener() {
// pass filter text to listener
                        public void actionPerformed(ActionEvent e) {
                            String text = filterText.getText();

                            if (text.length() == 0) {
                                sorter.setRowFilter(null);
                            } else {
                                try {
                                    sorter.setRowFilter(
                                            RowFilter.regexFilter(text));

                                } // end try
                                catch (PatternSyntaxException pse) {
                                    JOptionPane.showMessageDialog(null,
                                            "Bad regex pattern", "Bad regex pattern",
                                            JOptionPane.ERROR_MESSAGE);
                                } // end catch
                            } // end else
                        } // end method actionPerfomed
                    } // end annonymous inner class
                    ); // end call to addActionLister
        } // end try
        catch (SQLException sqlException) {
            JOptionPane.showMessageDialog(null, sqlException.getMessage(),
                    "Database error", JOptionPane.ERROR_MESSAGE);

// ensure database connection is closed
            tableModel.disconnectFromDatabase();
            System.exit(1); // terminate application
        } // end catch

        // dispose of window when user quits application (this overrides
        // the default of HIDE_ON_CLOSE)
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // ensure database connection is closed when user quits application
        addWindowListener(
                new WindowAdapter() {
                    // disconnect from database and exit when window has closed
                    public void windowClosed(WindowEvent event) {
                        tableModel.disconnectFromDatabase();
//                        System.exit(0);
                    } // end method windowClosed
                } // end WindowAdapter inner class
                ); // end call to addWindowListener
    } // end DisplayQueryResults constructor
    // execute application
    // execute applicationen
//    public static void main(String args[]) {
//        new DisplayQueryResults();
//    } // end main
//  public void SetData(Object obj, int row_index, int col_index){
//  resultTable.getModel().setValueAt(obj,row_index,col_index);
//  System.out.println("Value is added");
//}
} // end class DisplayQueryResults